//
//  ZZCAddAddressController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/24.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCAddAddressController.h"

@interface ZZCAddAddressController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UIView *areaView;
@property (weak, nonatomic) IBOutlet UITextField *addressDetailsTextField;
@property (weak, nonatomic) IBOutlet UIView *defaultAddressView;
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (assign, nonatomic) BOOL isDefaultAddress;//是否点了默认地址
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;
@property (nonatomic, strong) CityChoose *cityChoose;    /** 城市选择 */
@property (copy, nonatomic) NSString *province;
@property (copy, nonatomic) NSString *city;
@property (copy, nonatomic) NSString *area;
@end

@implementation ZZCAddAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加新地址";
    self.view.backgroundColor = ZZCBGColor;
    [self addGesturesToControls];
    self.isDefaultAddress = NO;
    [self.nameTextField becomeFirstResponder];
}

//给控件添加手势
- (void)addGesturesToControls {
    self.areaView.userInteractionEnabled = YES;
    UITapGestureRecognizer *areaTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(areaTapClick)];
    [self.areaView addGestureRecognizer:areaTap];
    
    self.defaultAddressView.userInteractionEnabled = YES;
    UITapGestureRecognizer *defaultAddressTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(defaultAddressTapClick)];
    [self.defaultAddressView addGestureRecognizer:defaultAddressTap];
}
//监听控件点击
- (void)areaTapClick {
    ZZCLog(@"你点击了地区");
    ZZCTempController *Temp = [ZZCTempController new];
    Temp.title = @"地区";
//    [self.navigationController pushViewController:Temp animated:YES];
    self.cityChoose = [[CityChoose alloc] init];
    __weak typeof(self) weakSelf = self;
    self.cityChoose.config = ^(NSString *province, NSString *city, NSString *town){
        weakSelf.areaLabel.text = [NSString stringWithFormat:@"%@-%@-%@",province,city,town];
        weakSelf.province = province;
        weakSelf.city = city;
        weakSelf.area = town;
    };
    [self.view addSubview:self.cityChoose];
    
}

- (void)requestAreaData {
    if ([_areaLabel.text isEqualToString:@"请选择"]) {
        [SVProgressHUD showInfoWithStatus:@"请选择地区"];
        return;
    }
    int is_default;
    if (!self.isDefaultAddress) {
        is_default = 2;
    } else {
        is_default = 1;
    }
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"province":self.province,
                           @"city":self.city,
                           @"area":self.area,
                           @"street":self.addressDetailsTextField.text,
                           @"receiver":self.nameTextField.text,
                           @"tel":self.phoneTextField.text,
                           @"is_default":@(is_default),
                           @"token":token
                           };
    NSLog(@"$$$$$$$$$%d",self.isDefaultAddress);
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
                            @"province":self.province,
                            @"city":self.city,
                            @"area":self.area,
                            @"street":self.addressDetailsTextField.text,
                            @"receiver":self.nameTextField.text,
                            @"tel":self.phoneTextField.text,
                            @"is_default":@(is_default),
                            @"token":token};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_NEWADDRESS parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
//            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            if ([responseObject[@"code"] integerValue] == 400) {
                [UIAlertController showInfoAlertLoginVC:self title:@"提示" message:responseObject[@"msg"] ok:nil];
            }else {
                [self.navigationController popViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"addressUpdate" object:self];
            }
        }
    }];
}


- (void)defaultAddressTapClick {
    ZZCLog(@"你点击了默认地址");
    self.isDefaultAddress = !self.isDefaultAddress;
    if (self.isDefaultAddress) {
        self.imgView.image = [UIImage imageNamed:@"圆_pressed"];
    }else {
        self.imgView.image = [UIImage imageNamed:@"圆_normal"];
    }
}

//保存
- (IBAction)preserveClick {
    ZZCLog(@"你点击了保存");
    if (self.addressDetailsTextField.text.length == 0) {
          [UIAlertController showInfoAlertLoginVC:self title:@"提示" message:@"详细地址不能为空" ok:nil];
    }else {
        [self requestAreaData]; 
    }
  
  
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
